package util;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.tomcat.dbcp.dbcp.BasicDataSource;

public class DBUtil {
	private BasicDataSource bd;
	private static DBUtil instance;
	private DBUtil(){
		bd = new BasicDataSource();
		//DB 연결 관련 설정
		bd.setDriverClassName("oracle.jdbc.driver.OracleDriver");//DriverClass 설정
		bd.setUrl("jdbc:oracle:thin:@127.0.0.1:1521:XE");//Url 설정
		bd.setUsername("scott");
		bd.setPassword("tiger");//계정 설정
		//Connection Pool 관련 설정 - option
		bd.setMaxActive(10);//최대 생성할 Connection의 개수 - 기본 : 8
		bd.setMaxIdle(2);//대체 가능한 Connection의 최대수 - 기본 : 8
		bd.setMinIdle(1);//대체가능한 Connection의 최소수 - 기본 : 0
		bd.setMaxWait(1000);//대여가능한 connection이 없을 때 Connection이 반납될 때 까지 기다릴 시간 (단위: 밀리초)//기본 : 무한대로 기다림
	}
	
	public static DBUtil getInstance(){
		if(instance == null){
			instance = new DBUtil();
		}
		return instance;
	}
	
	public Connection getConnection() throws SQLException{
		return bd.getConnection();
	}
	public void close(Connection conn, PreparedStatement stmt) throws SQLException{
		if(stmt!=null) stmt.close();
		if(conn!=null) conn.close();//반납
	}
	public void close(Connection conn, PreparedStatement stmt, ResultSet rset) throws SQLException{
		if(rset!=null) rset.close();
		if(stmt!=null) stmt.close();
		if(conn!=null) conn.close();//반납
	}
}
